package com.lx.user.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.bean.Result;
import com.lx.bilibili.entity.ReUpFens;
import com.lx.bilibili.entity.User;
import com.lx.user.dao.ReUpFensMapper;
import com.lx.user.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("reupfens")
public class ReUpFensController {

    @Autowired
    ReUpFensMapper reupFensMapper;

    @Autowired
    UserMapper userMapper;

    @RequestMapping("subscribe")
    @Transactional
    public int subscribe(String upid, String uid, Boolean isSubscribe) {
        LambdaQueryWrapper<ReUpFens> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ReUpFens::getUpid, upid);
        lqw.eq(ReUpFens::getUid,uid);
        ReUpFens reUpFens = reupFensMapper.selectOne(lqw);
        if(reUpFens == null){
        // 点击关注
        if (isSubscribe) {
            ReUpFens reupFens = new ReUpFens();
            reupFens.setUid(uid);
            reupFens.setUpid(upid);
            reupFensMapper.insert(reupFens);
            // 粉丝数加一
            LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
            luw.eq(User::getId, upid).setSql("fens = fens + 1"); // 使用setSql来执行自增操作
            userMapper.update(luw);
            // 用户关注数加一
            LambdaUpdateWrapper<User> luw2 = new LambdaUpdateWrapper<>();
            luw2.eq(User::getId, uid).setSql("subs = subs + 1"); // 使用setSql来执行自增操作
            return userMapper.update(luw2);
        } else {
            return 0;
        }
        }else{
            if(!isSubscribe){
                // 关注了， 取关
                // 删除up-fens关系表
                LambdaUpdateWrapper<ReUpFens> luw = new LambdaUpdateWrapper<>();
                luw.eq(ReUpFens::getUpid, upid);
                luw.eq(ReUpFens::getUid, uid);
                reupFensMapper.delete(luw);
                // 粉丝数减一
                LambdaUpdateWrapper<User> luw2 = new LambdaUpdateWrapper<>();
                luw2.eq(User::getId, upid).setSql("fens = fens - 1");
                // 用户关注数减一
                LambdaUpdateWrapper<User> luw3 = new LambdaUpdateWrapper<>();
                luw3.eq(User::getId, uid).setSql("subs = subs - 1"); // 使用setSql来执行自增操作
                userMapper.delete(luw3);
                return userMapper.update(luw2);
            }else{
                return 0;
            }

        }
    }

    @RequestMapping("getSubscribeRelation")
    public ReUpFens getSubscribeRelation(String upid, String uid){
        LambdaQueryWrapper<ReUpFens> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ReUpFens::getUpid, upid);
        lqw.eq(ReUpFens::getUid,uid);
        return reupFensMapper.selectOne(lqw);
    }
}
